﻿using HalconDotNet;
using MvCamCtrl.NET;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YTE.Control;
using 右下角弹窗.DiagShow;

namespace LD.Vision
{
    public partial class FrmROI : FrmBase
    {
        private AbsCamera camera;
        public FrmROI()
        {
            InitializeComponent();
            this.Shown += FrmCam_Shown;
        }

        private void FrmCam_Shown(object sender, EventArgs e)
        {
            foreach (var item in CameraFactory.Instance.CameraList)
            {
                this.cob_camera.Items.Add(item.Key);
            }
        }
        private void btn_saveImage_Click(object sender, EventArgs e)
        {
            if (this.cob_camera.SelectedIndex > -1)
            {
                FrmVisionMain.Instance.HWindow[camera.Name].viewWindow.genRect1(110.0, 110.0, 210.0, 210.0, ref camera.Roi);
                camera.Roi.Last().Color = "blue";
                binDataGridView(this.dgv_ROI, camera.Roi);
            }
        }

        void binDataGridView(DataGridView dgv, List<ViewWindow.Model.ROI> config)
        {
            try
            {
                dgv.DataSource = null;
                dgv.DataSource = config;
                //foreach (var item in config)
                //{
                //    dgv.Rows.Add(item.GetType(), item.Color, item.Msize);
                //}
                DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
                column1.DataPropertyName = "Type";
                column1.HeaderText = "类型";
                column1.Name = "Type";
                column1.Width = 90;
                column1.ReadOnly = true;

                //dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { column1 });
                dgv.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dgv.AllowUserToDeleteRows = true;
                dgv.AllowUserToAddRows = false;
                dgv.MultiSelect = false;
                dgv.AllowUserToAddRows = false;//禁止添加行
                dgv.AllowUserToDeleteRows = true;//禁止删除行
                dgv.Refresh();
                if (config.Count > 0)
                {
                   dgv.Rows[config.Count - 1].Cells[0].Selected = true;

                    //var img = camera.HWindow.Image;
                    //camera.HWindow.HobjectToHimage(img);
                    //camera.HWindow.viewWindow.displayROI(config);

                    this.txt_name.Text = config.Count() + "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void cob_camera_SelectedIndexChanged(object sender, EventArgs e)
        {
            camera = CameraFactory.Instance.CameraList[this.cob_camera.Text];

            binDataGridView(this.dgv_ROI, camera.Roi);

            if (camera.Roi.Count > 0)
            {
                var img = FrmVisionMain.Instance.HWindow[camera.Name].Image;
                FrmVisionMain.Instance.HWindow[camera.Name].HobjectToHimage(img);
                FrmVisionMain.Instance.HWindow[camera.Name].viewWindow.displayROI(camera.Roi);
            }
        }
        public void Save()
        {
            if (camera != null && FrmVisionMain.Instance.HWindow[camera.Name] != null)
            {
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"Vision\{camera.Name}");
                string pathModel = Path.Combine(path, $@"Model");
                FrmVisionMain.Instance.HWindow[camera.Name].viewWindow.saveROI(camera.Roi, Path.Combine(pathModel, $"{camera.Name}.xml"));

                Dialog dialog = new Dialog();
                dialog.Success("保存成功");
            }
        }

        private void dgv_ROI_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (camera != null && FrmVisionMain.Instance.HWindow[camera.Name] != null && e.RowIndex > -1)
            {
                FrmVisionMain.Instance.HWindow[camera.Name].viewWindow.selectROI(e.RowIndex);     //这里只需要这一句就够了，
            }
        }

        private void 删除所选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (camera != null && FrmVisionMain.Instance.HWindow[camera.Name] != null)
            {
                FrmVisionMain.Instance.HWindow[camera.Name].viewWindow.removeActiveROI(ref camera.Roi);     //这里只需要这一句就够了，
                binDataGridView(this.dgv_ROI, camera.Roi);
            }
        }

        private void btn_line_Click(object sender, EventArgs e)
        {
            if (this.cob_camera.SelectedIndex > -1)
            {
                FrmVisionMain.Instance.HWindow[camera.Name].viewWindow.genLine(100.0, 100.0, 100.0, 100.0, ref camera.Roi);
                camera.Roi.Last().Color = "blue";
                binDataGridView(this.dgv_ROI, camera.Roi);
            }
        }

        private void btn_cir_Click(object sender, EventArgs e)
        {
            if (this.cob_camera.SelectedIndex > -1)
            {
                FrmVisionMain.Instance.HWindow[camera.Name].viewWindow.genCircle(200.0, 200.0, 60.0, ref camera.Roi);
                camera.Roi.Last().Color = "blue";
                binDataGridView(this.dgv_ROI, camera.Roi);
            }
        }

        private void btn_genRect2_Click(object sender, EventArgs e)
        {
            if (this.cob_camera.SelectedIndex > -1)
            {
                FrmVisionMain.Instance.HWindow[camera.Name].viewWindow.genRect2(200.0, 200.0, 30.0 / 180.0 * Math.PI, 60.0, 30.0, ref camera.Roi);
                camera.Roi.Last().Color = "blue";
                binDataGridView(this.dgv_ROI, camera.Roi);
            }
        }
    }
}
